﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebSite.ErrorMails;
using WebSite.NewsHandle;

public partial class WebSite_ImportantInfoList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                NewsHandler newsHandler = new NewsHandler("Data Source=Localhost;Integrated Security=SSPI;Initial Catalog=CSHDB");
                DataSet ds = newsHandler.GetNewBySection(1, 20, 1);
                NewsDataList.DataSource = ds;
                NewsDataList.DataBind();
                this.ViewState["PageIndex"] = "1";
                this.ViewState["TotalNum"] = newsHandler.GetCountOfSectionNews(1).ToString();
                PreviousPageBtn.Enabled = false;
                if (int.Parse(this.ViewState["TotalNum"].ToString()) > 20)
                {
                    NextPageBtn.Enabled = true;
                }
                else NextPageBtn.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            IfError(ex);
        }
    }
    protected void PreviousPageBtn_Click(object sender, EventArgs e)
    {
        try
        {
            int PageIndex = int.Parse(this.ViewState["PageIndex"].ToString()) - 1;
            int count = int.Parse(this.ViewState["TotalNum"].ToString());
            if (PageIndex == 1)
            {
                PreviousPageBtn.Enabled = false;
            }
            NewsHandler newsHandler = new NewsHandler("Data Source=Localhost;Integrated Security=SSPI;Initial Catalog=CSHDB");
            DataSet ds = newsHandler.GetNewBySection(1, 20, PageIndex);
            NewsDataList.DataSource = ds;
            NewsDataList.DataBind();
        }
        catch (Exception ex)
        {
            IfError(ex);
        }
    }

    protected void NextPageBtn_Click(object sender, EventArgs e)
    {
        try
        {
            int PageIndex = int.Parse(this.ViewState["PageIndex"].ToString()) + 1;
            int count = int.Parse(this.ViewState["TotalNum"].ToString());

            PreviousPageBtn.Enabled = true;

            if (PageIndex * 20 >= count)
            {
                NextPageBtn.Enabled = false;
            }
            NewsHandler newsHandler = new NewsHandler("Data Source=Localhost;Integrated Security=SSPI;Initial Catalog=CSHDB");
            DataSet ds = newsHandler.GetNewBySection(1, 20, PageIndex);
            NewsDataList.DataSource = ds;
            NewsDataList.DataBind();
        }
        catch (Exception ex)
        {
            IfError(ex);
        }
    }
    private void IfError(Exception ex)
    {
        string userid;
        if (Session["UserId"] != null)
        {
            userid = Session["UserId"].ToString();
        }
        else userid = "游客";
        ErrorMailSend MailSender = new ErrorMailSend();
        MailSender.SendErrorMail("Message:" + ex.Message + "\nStackTrace:" + ex.StackTrace + "\nSource:" + ex.Source, userid, "HealthKnowledgeList.aspx");
        MailSender.WriteIntoXml("Message:" + ex.Message + "\nStackTrace:" + ex.StackTrace + "\nSource:" + ex.Source, userid, "HealthKnowledgeList.aspx");
        Response.Redirect("Default.aspx", false);
        HttpContext.Current.ApplicationInstance.CompleteRequest();
        return;
    }
}
